﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Buy2Site.MasterPages;
using Buy2Site.UserControls;
using Buy2Site.Classes;
using Buy2Site.Enums;

namespace Buy2Site.Pages
{
    public partial class ProductDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ((MySiteMaster)Master).PageTitle = "Product Details";
            //Page.Title = "Product Details";
            ((MySiteMaster)Master).HideLink(LinksEnum.ProductDetailsLink);

            // case user is logged in
            if (Session["User"] != null)
            {
                ((MySiteMaster)Master).LogInVisible = false;
                ((MySiteMaster)Master).HelloUserVisible = true;
            }
            // case user is not logged in
            else
            {
                ((MySiteMaster)Master).LogInVisible = true;
                ((MySiteMaster)Master).HelloUserVisible = false;
            }

            string productID = Request.QueryString["productID"];
            int id;
            bool pBool = int.TryParse(productID, out id);
            if (pBool)
            {
                using (Buy2SiteDBEntities ent = new Buy2SiteDBEntities())
                {
                    Product product = ent.Product.FirstOrDefault(x => x.ID == id);
                    int i = 1;
                    if (product != null && product.Picture1 != null)
                    {
                        ProductDetailsControl productDetailsControl = (ProductDetailsControl)LoadControl("~/UserControls/ProductDetailsControl.ascx");
                        ((ProductDetailsControl)productDetailsControl).Title = product.Title;
                        ((ProductDetailsControl)productDetailsControl).Price = product.Price;
                        ((ProductDetailsControl)productDetailsControl).Date = product.Date;
                        ((ProductDetailsControl)productDetailsControl).ShortDescription = product.ShortDescription;
                        ((ProductDetailsControl)productDetailsControl).LongDescription = product.LongDescription;
                        ((ProductDetailsControl)productDetailsControl).Image1 =
                            "~/Handlers/HandlerAddProductImageToWebSite.ashx?productID=" + product.ID + "&pictureNumerID=" + i;
                        ((ProductDetailsControl)productDetailsControl).Image2 =
                             "~/Handlers/HandlerAddProductImageToWebSite.ashx?productID=" + product.ID + "&pictureNumerID=" + (++i);
                        ((ProductDetailsControl)productDetailsControl).Image3 =
                            "~/Handlers/HandlerAddProductImageToWebSite.ashx?productID=" + product.ID + "&pictureNumerID=" + (++i);


                        this.productsPlaceHolder.Controls.Add(productDetailsControl);
                        //context.Response.OutputStream.Write(product.Picture1, 0, product.Picture1.Length);

                        // get user of ad
                        //User user = ent.User.FirstOrDefault(x => x.ID == ent.Product.FirstOrDefault(x=>x.UserID==);

                        //var user = from u in ent.User
                        //           from p in ent.Product
                        //           where u.ID == p.UserID
                        //           select u;
                        
                        User myUser=new User();

                        foreach (var u in ent.User)
                        {
                            foreach (var p in ent.Product)
                            {
                                if (u.ID == p.UserID)
                                {
                                    myUser = u;
                                    break;
                                }
                            }
                            break;
                        }

                        lblPublishedByUser.Text = "Published by: " + myUser.FirstName + " " +
                                                    myUser.LastName + " , " +
                                                    "Email: " + myUser.Email;

                        //lblPublishedByUser.Text = "Published by: " + ((UserClass)user).FirstName + "," +
                        //                            ((UserClass)user).LastName + "," +
                        //                            ((UserClass)user).FirstName + "," +
                        //                            "Email: " + ((UserClass)user).Email;

                    }
                    else
                    {
                        Response.Redirect("~/Pages/Home.aspx");
                    }
                }
            }
            else if(productID==null || productID=="")
            {
                Uri noItemUrlReffere = Request.UrlReferrer;
                if(noItemUrlReffere!=null)
                    Response.Redirect(noItemUrlReffere.AbsoluteUri);
            }            
        }

        //protected void goToMainEvent(object sender, EventArgs e)
        //{
        //    //string commandName = (sender as LinkButton).CommandName;
        //    //if (commandName == "GoToMain")
        //        Response.Redirect("~/Pages/Home.aspx");
        //}
    }
}